package org.stvd.core.util.convert;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.core.convert.converter.Converter;

public class DateConverter implements Converter<String, Date> {

    private static final List<String> formarts = new ArrayList<String>(4);
    static {
        formarts.add("yyyy-MM");
        formarts.add("yyyy年MM月");
        formarts.add("yyyy-MM-dd");
        formarts.add("yyyy.MM.dd");
        formarts.add("yyyy年MM月dd日");
        formarts.add("yyyy-MM-dd HH:mm");
        formarts.add("yyyy年MM月dd日 HH时mm分");
        formarts.add("yyyy-MM-dd HH:mm:ss");
        formarts.add("yyyy年MM月dd日 HH时mm分ss秒");
        formarts.add("yyyyMMdd");
        formarts.add("HHmmss");
        formarts.add("yyyyMMddHHmmss");
        formarts.add("yyyyMMddHHmmssSSS");
        formarts.add("HH:mm:ss");
        formarts.add("HH时mm分ss秒");
    }

    public Date convert(String source) {
        String value = source.trim();
        if ("".equals(value)) {
            return null;
        }
        if (source.matches("^\\d{4}-\\d{1,2}$")) {
            return parseDate(source, formarts.get(0));
        } else if (source.matches("^\\\\d{4}年\\\\d{1,2}月$\"")) {
            return parseDate(source, formarts.get(1));
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2}$")) {
            return parseDate(source, formarts.get(2));
        } else if (source.matches("^\\d{4}.\\d{1,2}.\\d{1,2}$")) {
            return parseDate(source, formarts.get(3));
        } else if (source.matches("^\\d{4}年\\d{1,2}月\\d{1,2}日$")) {
            return parseDate(source, formarts.get(4));
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}$")) {
            return parseDate(source, formarts.get(5));
        } else if (source.matches("^\\d{4}年\\d{1,2}月\\d{1,2}日 {1}\\d{1,2}时\\d{1,2}分$")) {
            return parseDate(source, formarts.get(6));
        } else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
            return parseDate(source, formarts.get(7));
        }  else if (source.matches("^\\d{4}-\\d{1,2}-\\d{1,2} {1}\\d{1,2}:\\d{1,2}:\\d{1,2}.0$")) {
            return parseDate(source, formarts.get(7));
        } else if (source.matches("^\\d{4}年\\d{1,2}月\\d{1,2}日 {1}\\d{1,2}时\\d{1,2}分\\d{1,2}秒$")) {
            return parseDate(source, formarts.get(8));
        } else if (source.matches("^\\d{8}$")) {
            return parseDate(source, formarts.get(9));
        } else if (source.matches("^\\d{6}$")) {
            return parseDate(source, formarts.get(10));
        } else if (source.matches("^\\d{14}$")) {
            return parseDate(source, formarts.get(11));
        } else if (source.matches("^\\d{17}$")) {
            return parseDate(source, formarts.get(12));
        } else if (source.matches("^\\d{1,2}:\\d{1,2}:\\d{1,2}$")) {
            return parseDate(source, formarts.get(13));
        } else if (source.matches("^\\d{1,2}时\\d{1,2}分\\d{1,2}秒$")) {
            return parseDate(source, formarts.get(13));
        } else {
            throw new IllegalArgumentException("Invalid Date value '" + source + "'");
        }
    }

    /**
     * 功能描述：格式化日期
     * 
     * @param dateStr
     *            String 字符型日期
     * @param format
     *            String 格式
     * @return Date 日期
     */
    public Date parseDate(String dateStr, String format) {
        Date date = null;
        try {
            DateFormat dateFormat = new SimpleDateFormat(format);
            date = (Date) dateFormat.parse(dateStr);
        } catch (Exception e) {
        }
        return date;
    }

}